package com.dense.kuiniu.bookborrow.setting;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.base.SpKey;
import com.dense.kuiniu.bookborrow.base.Url;
import com.dense.kuiniu.bookborrow.net.SocketChecker;
import com.dense.kuiniu.bookborrow.xjSetting.manager.PingBaoManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @author Administrator
 * @date 2021/2/2
 */
public class SettingActivity extends AppCompatActivity {
    private EditText mEtAddress;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.delegate_setting);
        mEtAddress = findViewById(R.id.et_address);
        TextView tvVersion = findViewById(R.id.tv_version);
        tvVersion.setText(String.format("版本：%s", AppUtils.getAppVersionName()));
        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("设置");
        findViewById(R.id.tv_return).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.tv_address_modify).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyAddress();
            }
        });
        String url = SPUtils.getInstance().getString(SpKey.KEY_BASE_URL);
        if (url.isEmpty()) {
            url = Url.baseUrl;
        }
        mEtAddress.setText(url);
    }

    private void modifyAddress() {
        String address = mEtAddress.getText().toString();
        if (address.isEmpty()) {
            ToastUtils.showShort("地址不能为空");
        } else {
            SPUtils.getInstance().put(SpKey.KEY_BASE_URL, address);
            Url.baseUrl = address;
            SocketChecker.getInstance().changeAddress();
            ToastUtils.showShort("设置成功");

        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        PingBaoManager.getInstance().updateClickTime();
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {
                hideSoftInput(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left + v.getWidth();
            if (event.getRawX() > left && event.getRawX() < right && event.getRawY() > bottom && event.getRawY() < top) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    private void hideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public void exit(View view) {
        startActivity(new Intent(Settings.ACTION_SETTINGS));
    }

    public void browser(View view) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse("http://www.baidu.com");
        intent.setData(content_url);
        startActivity(intent);
    }

}